package com.cdsxlc.common.utils;

import com.cdsxlc.common.constant.GeneratorConstant;
import com.cdsxlc.common.entity.TableColumns;
import com.cdsxlc.common.enums.DataTypeEnums;
import org.apache.commons.collections.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhang
 * 内容生成工具
 */
public class GenerateUtils {

    /**
     * 实体内容
     *
     * @param tableColumnsList
     * @return
     */
    public static Map<String, String> getEntityData(List<TableColumns> tableColumnsList) {
        StringBuilder propertyString = new StringBuilder();
        Map<String, String> resultMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(tableColumnsList)) {
            StringBuilder referencePackage = new StringBuilder("");
            for (TableColumns tableColumns : tableColumnsList) {
                /**
                 * 添加注解
                 */
                propertyString.append("\t@Column(name= \"").append(tableColumns.getColumnName())
                        .append("\", ")
                        .append("columnDefinition = \"")
                        .append(tableColumns.getColumnType())
                        .append(" comment '")
                        .append(tableColumns.getColumnComment())
                        .append("'\")\n");

                /**
                 * 添加属性
                 */
                DataTypeEnums javaDataTypeByMysqlDataType = DataTypeEnums.getJavaDataTypeByMysqlDataType(tableColumns.getDataType());
                if (StringUtils.isNotBlank(javaDataTypeByMysqlDataType.getReferenceJar())) {
                    referencePackage.append(javaDataTypeByMysqlDataType.getReferenceJar());
                }
                propertyString.append("\tprivate ")
                        .append(javaDataTypeByMysqlDataType.getJavaDataType())
                        .append(" ")
                        .append(StringUtils.formatNameCamel(tableColumns.getColumnName()))
                        .append(";\n\n");
            }
            resultMap.put("referencePackage", referencePackage.toString());
            resultMap.put("entityData", propertyString.toString());
        }
        return resultMap;
    }

    public static Map<String, String> getDTOData(List<TableColumns> tableColumnsList) {
        StringBuilder propertyString = new StringBuilder();
        Map<String, String> resultMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(tableColumnsList)) {
            StringBuilder referencePackage = new StringBuilder("");
            for (TableColumns tableColumns : tableColumnsList) {
                /**
                 * 添加校验
                 */
                if (StringUtils.isNotBlank(tableColumns.getColumnLength())) {
                    propertyString.append("\t");
                    propertyString.append("@Length(max = ")
                            .append(tableColumns.getColumnLength())
                            .append(",")
                            .append(" message=\"")
                            .append(tableColumns.getColumnComment())
                            .append("长度不能超过")
                            .append(tableColumns.getColumnLength())
                            .append("个字符")
                            .append("\")\n");
                    referencePackage.append("import org.hibernate.validator.constraints.Length;\n");
                }
                if (StringUtils.equals(tableColumns.getDataType(), "decimal")) {
                    propertyString.append("\t");
                    propertyString.append("@Digits(integer = ")
                            .append(tableColumns.getNumericPrecision() - tableColumns.getNumericScale())
                            .append(",")
                            .append(" fraction = ")
                            .append(tableColumns.getNumericScale())
                            .append(",")
                            .append(" message=\"")
                            .append(tableColumns.getColumnComment())
                            .append("应在0至").append(BigDecimal.TEN.pow(tableColumns.getNumericPrecision() - tableColumns.getNumericScale()))
                            .append("之间\")\n");
                    referencePackage.append("import javax.validation.constraints.Digits;\n");
                }

                /**
                 * 添加swagger注解
                 */
                propertyString.append("\t@ApiModelProperty(value = \"")
                        .append(tableColumns.getColumnComment());
                if (StringUtils.equals(tableColumns.getDataType(), "varchar")) {
                    propertyString.append("最大长度").append(tableColumns.getColumnLength());
                }

                if (StringUtils.equals(tableColumns.getDataType(), "decimal")) {
                    propertyString.append("(数字）整数位")
                            .append(tableColumns.getNumericPrecision() - tableColumns.getNumericScale())
                            .append("，小数位").append(tableColumns.getNumericScale());

                }
                propertyString.append("\")\n");

                /**
                 * 添加属性
                 */
                DataTypeEnums javaDataTypeByMysqlDataType = DataTypeEnums.getJavaDataTypeByMysqlDataType(tableColumns.getDataType());
                if (StringUtils.isNotBlank(javaDataTypeByMysqlDataType.getReferenceJar())) {
                    referencePackage.append(javaDataTypeByMysqlDataType.getReferenceJar());
                }

                if (javaDataTypeByMysqlDataType.getJavaDataType().equals("Date")) {
                    propertyString.append("\t@JsonFormat( pattern = ")
                            .append("Constant.DATE_FORMAT ")
                            .append(", timezone = Constant.TIMEZONE)\n");
                    referencePackage.append("import com.fasterxml.jackson.annotation.JsonFormat;\n");
                    referencePackage.append("import "+ GeneratorConstant.PACKAGE_PREFIX + "constant.Constant;\n");
                }
                propertyString.append("\tprivate  ")
                        .append(javaDataTypeByMysqlDataType.getJavaDataType())
                        .append(" ")
                        .append(StringUtils.formatNameCamel(tableColumns.getColumnName()))
                        .append(";\n\n  ");

            }
            resultMap.put("dtoData", propertyString.toString());
            referencePackage.append("import "+ GeneratorConstant.PACKAGE_PREFIX + "dto.BaseDTO;\n");
            resultMap.put("referencePackage", referencePackage.toString());
        }
        return resultMap;
    }

    /**
     * 生成vo 数据
     *
     * @param tableColumnsList
     * @return
     */
    public static Map<String, String> getVoData(List<TableColumns> tableColumnsList) {
        StringBuilder propertyString = new StringBuilder();
        Map<String, String> resultMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(tableColumnsList)) {
            StringBuilder referencePackage = new StringBuilder("");
            for (TableColumns tableColumns : tableColumnsList) {
                /**
                 * 添加swagger注解
                 */
                propertyString.append("\t@ApiModelProperty(value = \"")
                        .append(tableColumns.getColumnComment()).append("\")\n");
                /**
                 * 添加属性
                 */
                DataTypeEnums javaDataTypeByMysqlDataType = DataTypeEnums.getJavaDataTypeByMysqlDataType(tableColumns.getDataType());
                if (javaDataTypeByMysqlDataType.getJavaDataType().equals("Date")) {
                    propertyString.append("\t@JsonFormat( pattern = ")
                            .append("Constant.DATE_FORMAT ")
                            .append(", timezone = Constant.TIMEZONE)\n");
                    referencePackage.append("import com.fasterxml.jackson.annotation.JsonFormat;\n");
                    referencePackage.append("import "+ GeneratorConstant.PACKAGE_PREFIX + "constant.Constant;\n");
                }
                if (StringUtils.isNotBlank(javaDataTypeByMysqlDataType.getReferenceJar())) {
                    referencePackage.append(javaDataTypeByMysqlDataType.getReferenceJar());
                }
                propertyString.append("\tprivate  ")
                        .append(javaDataTypeByMysqlDataType.getJavaDataType())
                        .append(" ")
                        .append(StringUtils.formatNameCamel(tableColumns.getColumnName()))
                        .append(";\n\n");

            }
            resultMap.put("voData", propertyString.toString());
            resultMap.put("referencePackage", referencePackage.toString());
        }
        return resultMap;
    }
}
